package com.ke.searchhouse.controller.house;

import com.ke.searchhouse.base.Response;
import com.ke.searchhouse.dto.SubwayDTO;
import com.ke.searchhouse.dto.SubwayStationDTO;
import com.ke.searchhouse.dto.SupportAddressDTO;
import com.ke.searchhouse.service.IAddressService;
import com.ke.searchhouse.util.ServiceMultiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/address/support")
public class AddressController {

    @Autowired
    private IAddressService addressService;

    @GetMapping("/cities")
    @ResponseBody
    public Response getSupportCitys() {
        ServiceMultiResult<SupportAddressDTO> supportAddress = addressService.finAllCities();
        if (0 == supportAddress.getResultSize()) {
            return Response.ofStatus(Response.Status.NOT_FOUND);
        }

        return Response.ofSuccess(supportAddress.getResult());
    }

    /**
     * 获取对应城市支持区域列表
     * @param cityEnName
     * @return
     */
    @GetMapping("/regions")
    @ResponseBody
    public Response getSupportRegions(@RequestParam(name = "city_name") String cityEnName) {
        ServiceMultiResult<SupportAddressDTO> addressResult = addressService.findAllRegionsByCityName(cityEnName);
        if (addressResult.getResult() == null || addressResult.getTotal() < 1) {
            return Response.ofStatus(Response.Status.NOT_FOUND);
        }
        return Response.ofSuccess(addressResult.getResult());
    }

    /**
     * 获取具体城市所支持的地铁线路
     * @param cityEnName
     * @return
     */
    @GetMapping("/subway/line")
    @ResponseBody
    public Response getSupportSubwayLine(@RequestParam(name = "city_name") String cityEnName) {
        List<SubwayDTO> subways = addressService.findAllSubwayByCity(cityEnName);
        if (subways.isEmpty()) {
            return Response.ofStatus(Response.Status.NOT_FOUND);
        }

        return Response.ofSuccess(subways);
    }

    /**
     * 获取对应地铁线路所支持的地铁站点
     * @param subwayId
     * @return
     */
    @GetMapping("/subway/station")
    @ResponseBody
    public Response getSupportSubwayStation(@RequestParam(name = "subway_id") Long subwayId) {
        List<SubwayStationDTO> stationDTOS = addressService.findAllStationBySubway(subwayId);
        if (stationDTOS.isEmpty()) {
            return Response.ofStatus(Response.Status.NOT_FOUND);
        }

        return Response.ofSuccess(stationDTOS);
    }

}
